//
//  SemDetailViewController.m
//  Gradekeeper0.2
//
//  Created by Ben Wu on 11/18/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SDetailViewController.h"
//#import "Course1h.h"
//#import "Gradekeeper0_2AppDelegate.h"
//#import "Profile.h"

#import <Foundation/NSObject.h>




//SDetailViewController created the semester table for selected year from SemTableView

@implementation SDetailViewController

@synthesize courseController, creationController, currentRow, setInfo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil semester:(Semester *)newSemester {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        semester = newSemester;
		setInfo = NO;
    }
    return self;
}

//programatically give edit and creation buttons
-(void)viewDidLoad{
	[super viewDidLoad];
	
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Create" 
																     style:UIBarButtonItemStyleBordered 
																    target:self 
																    action:@selector(createPressed:)];
	
	UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"Info"
																   style:UIBarButtonItemStyleBordered
																  target:self 
																  action:@selector(infoPressed:)];
	
	self.toolbarItems = [NSArray arrayWithObjects: self.editButtonItem, infoButton, nil];
	[infoButton release];
	
	courseList = [[NSMutableArray alloc] init];
	[courseList addObject: [[Courses alloc] initWithCatagory: @"cmpt"
													  number: 225
													  credit: 3
													   types: @"Project/MidTerms/Assignments"
													 weights: @"30/50/20"]];
	 
}

- (IBAction) infoPressed:(id)sender {
	setInfo = (!setInfo);
}

- (IBAction) createPressed:(id)sender {
	if (self.creationController == nil){
		self.creationController = [[CourseCreationViewController alloc] initWithNibName:@"CourseCreationViewController" bundle:nil semester:semester];
	}
	
	[self.navigationController pushViewController:creationController animated:YES];
	[self.creationController autorelease];
} //createPressed

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}



// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [courseList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	
	//set up each cell
	NSUInteger row = [indexPath row];
	cell.textLabel.text = [[courseList objectAtIndex:row] getName];
	return cell;
	
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 // Navigation logic may go here. Create and push another view controller.
 // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
 // [self.navigationController pushViewController:anotherViewController];
 // [anotherViewController release];
		
	NSInteger row = [indexPath row];
	NSString *selectedCourse = [NSString stringWithFormat:@"%@", [[courseList objectAtIndex:row] getName]];													   
	
	if (!setInfo){
		courseController = [[CourseWorkViewController alloc] initWithNibName:@"CourseWorkViewController" bundle:nil course:[courseList objectAtIndex:row]];
		courseController.title = selectedCourse;
		
		[self.navigationController pushViewController:courseController animated:YES];
		[courseController release];
		courseController = nil;
	} else {
		CourseDetailViewController *CDetailedView = [[CourseDetailViewController alloc] initWithNibName:@"CourseDetailViewController" bundle:nil course:[courseList objectAtIndex:row]];
		[self.navigationController pushViewController:CDetailedView animated:YES];
		[CDetailedView release];
	}
}

//Set up alert button function
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
	//If user confirms file deletion, the file will be removed from course.txt
	//The text file will also be removed from the respective directory and from the table view
	if ([alertView.title isEqualToString:@"Delete Confirmation"]){
		if (buttonIndex == 0) {
			
			/*NSFileManager  *NSFm;
			
			NSFm = [NSFileManager defaultManager] ;
			
			NSString *temp = [NSString stringWithFormat:@"%@", [[profile semesters] objectAtIndex:rowToDelete]];
			
			
			[NSFm removeItemAtPath:[fileIO getFilePath:temp] error:NULL];
			
			[fileIO deleteFromFile:@"Course" :temp :@":"];
			[[profile semesters] removeObjectAtIndex:rowToDelete];
			[self.tableView reloadData];*/
			
			[courseList removeObjectAtIndex:currentRow];
			[[semester courses] removeObjectAtIndex:currentRow];
			[self.tableView reloadData];
		}
	} 
} //alertView

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
	//If user chooses to delete a row, an alert box will pop up to confirm user's request
	//If user chooses yes, the file will be deleted. 
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		self.currentRow = indexPath.row;
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Delete Confirmation" 
							  message:@"Are you sure you would like to delete current year? \n All information stored under the this course will be deleted." 
							  delegate:self
							  cancelButtonTitle:@"Yes" 
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"No"];
		
		[alert show];
		
		[alert release];
		
		[self.tableView reloadData];
		
		
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // do nothing
    }   
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


- (void)dealloc {
	[[courseList objectAtIndex:0] release];
	[super dealloc];
}


@end


